package com.clj.blesample;

import com.clj.fastble.utils.HexUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by SEELE on 2018/1/16.
 */

public class Rp8CommondUtils extends BaseCommondUtils{
    @Override
    protected Map resolveNotify(byte[] notifyByte) {
        Map commondMap = new HashMap();
        //data1
        byte data1 = notifyByte[5];
        int[] btnStatus8 = HexUtil.getBit(data1);

        if (1 == btnStatus8[7]) {
            commondMap.put(R.id.btn_z1, 1);
        } else {
            commondMap.put(R.id.btn_z1, 0);
        }
        return commondMap;
    }

    /*private static boolean checkResponseCommond(String notify) {

        if(!notify.startsWith(Rp8Commond.HEX_COMMOND_RESPONSE_START)){
            return false;
        }
        return true;
    }*/

    @Override
    protected boolean check(String notify) {

        if (null == notify || notify.isEmpty()) {
            return false;
        }
        if (!notify.toUpperCase().startsWith(BaseCommond.HEX_COMMOND_START) || notify.length() <10) {
            return false;
        }
        int needCommondLength = needCommondLength(notify);
        if (notify.length() < needCommondLength) {
            return false;
        } else {
            notify = notify.substring(0, needCommondLength);
        }
        if (!crc8(notify.substring(0, notify.length() - 2)).equals(notify.substring(notify.length() - 2).toUpperCase())) {
            return false;
        }
        return true;
    }
    @Override
    protected boolean checkNotfiy(String notify) {

        if(!notify.substring(6, 8).toUpperCase().equals(BaseCommond.HEX_COMMOND_TYPE_NOTIFICATION_12)){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(crc8("55AA03120001"));  // 98
        System.out.println(crc8("55AA03120002"));  // 7A
        System.out.println(crc8("55AA03120004"));  // A7
        System.out.println(crc8("55AA03120008"));  // 04
        System.out.println(crc8("55AA03120010"));  // 5B
        System.out.println(crc8("55AA03120020"));  // E5
        System.out.println(crc8("55AA031201FF"));  // 37
        System.out.println(crc8("55AA031203FF"));  // A6
        System.out.println(crc8("55AA04120007ff"));  // 1D
    }
}
